package com.example.allshengmedical.system.activity;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.example.allshengmedical.util.ByAlert;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.SerialConfig;
import com.itboye.allshengmedical.sci.SerialPort;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

public class SCMUpdate {
	private OutputStream mOutputStream;
	private final static String FILEPATH = "/mnt/udisk/Gene32.bin";
	private final static String fileName = "mnt/udisk/";
	private final static String HaveUDisk = "/proc/partitions";
	SerialPort mSerialPort;

	public static boolean isHaveUDisk() {
		boolean isHave = false;
		File Usbfile = new File(HaveUDisk);
		if (Usbfile.exists()) {
			try {
				FileReader file = new FileReader(HaveUDisk);
				BufferedReader br = new BufferedReader(file);
				String strLine = "";
				while ((strLine = br.readLine()) != null) {
					if (strLine.indexOf("sd") > 0) {
						// 已经挂载，不排除有多个
						isHave = true;
						break;
					}
				}
				br.close();
				file.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return isHave;
	}

	public static String isHaveFile() {
		String strLine2 = null;
		File file = new File(FILEPATH);
		if (file.isFile() && file.exists()) {
			strLine2 = "have";
		} else {
			strLine2 = null;
		}
		return strLine2;
	}

	// public static byte[] Update() {
	// String strLine2 = "";
	// byte[] bs = null;
	// try {
	// String encoding = "GBK";
	// File file = new File(FILEPATH);
	// if (file.exists() && file.isFile()) { // 判断文件是否存在
	// InputStreamReader read = new InputStreamReader(
	// new FileInputStream(file));
	//
	// // ,encoding);// 考虑到编码格式
	// BufferedReader bufferedReader = new BufferedReader(read);
	// String lineTxt = null;
	//
	// StringBuilder builder = new StringBuilder();
	// while ((lineTxt = bufferedReader.readLine()) != null) {
	// builder.append(lineTxt);
	// }
	// bs = builder.toString().getBytes();
	// Log.e("hu", bs.length + "");
	// read.close();
	// } else {
	// ByAlert.alert("找不到指定的文件");
	// }
	//
	// } catch (IOException e) {
	// ByAlert.alert("读取文件内容出错");
	// e.printStackTrace();
	// }
	// return bs;
	// }

	// public void sendUpgradesData(byte data1[], int startset, int len) {
	// if (len <= 0) {
	// return;
	// }
	// byte data[] = new byte[len];
	// // data1:源数组； startset:源数组要复制的起始位置； data:目的数组； 0:目的数组放置的起始位置；
	// // len:复制的长度
	// System.arraycopy(data1, startset, data, 0, len);
	// sendCmd(data);
	// }

	public static void UpdateMainApp(Activity activity) {
		try {
			// usb3.0
			File file = new File(fileName);
			File[] files = file.listFiles();
			for (File f : files) {
				if (f.getName().contains(".apk")) {
					File apkfile = new File(fileName + f.getName());
					Intent i = new Intent(Intent.ACTION_VIEW);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
							"application/vnd.android.package-archive");
					activity.startActivity(i);
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			ByAlert.alert("请检查U盘，暂不支持USB3.0");
		}

	}

	// public static int upgradeMCUNum() {
	// String result = null;
	// int fileLen = 0;
	// byte[] upgradeData = new byte[65536];
	// File f1 = new File(FILEPATH);
	// if (f1.exists()) {
	// FileInputStream inStream = null;
	// long s = 0;
	// try {
	// inStream = new FileInputStream(f1);
	// s = inStream.available();
	// s = (s / 32 + 1) * 32;
	// byte buffer[] = new byte[(int) s];
	// int len = 0;
	// ByteArrayOutputStream ostream = new ByteArrayOutputStream();
	// while ((len = inStream.read(buffer)) != -1) {
	// ostream.write(buffer, 0, len);
	//
	// }
	// inStream.close();
	// upgradeData = ostream.toByteArray();
	// result = upgradeData.toString();
	// fileLen = upgradeData.length;
	// int offset = 0;
	// // sendStartUpgradesCmd(fileLen);
	// int checkSum = 0;
	// for (int i = 0; i < fileLen; i++) {
	// if (upgradeData[i] >= 0) {
	// checkSum += upgradeData[i];
	// } else {
	// checkSum += (upgradeData[i] + 256);
	// }
	// }
	// ostream.close();
	// } catch (IOException e) {
	// result = "升级出错";
	// }
	//
	// } else {
	// result = "没有升级文件";
	// }
	// return (fileLen);
	// }
	// public static void sendStartUpgradesCmd(int len) {
	// testCount = 0;
	//
	// int tmp;
	// byte aa[] = new byte[4];
	// aa[0] = (byte) (len & 0xff);
	// aa[1] = (byte) (len >> 8 & 0xff);
	//
	// byte cmd[] = new byte[11];
	// cmd[0] = 0x5a;
	// cmd[1] = (byte) 0xa5;
	// cmd[2] = (byte) 0x4;
	// cmd[3] = 0x06;
	// cmd[4] = 0x01;
	// cmd[5] = aa[1];
	// cmd[6] = aa[0];
	//
	// tmp = cmd[2] + cmd[3] + cmd[4] + cmd[5] + cmd[6];
	// aa[0] = (byte) (tmp & 0xff);
	// aa[1] = (byte) (tmp >> 8 & 0xff);
	// cmd[7] = aa[1];
	// cmd[8] = aa[0];
	//
	// cmd[9] = 0x3e;
	// cmd[10] = (byte) 0xe3;
	// lastCmd = 0x06;
	//
	// sendCmd(cmd);
	// }
}
